package com.example.springai.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/openai")
public class OpenAiChatController {
    private static final Logger log = LoggerFactory.getLogger(OpenAiChatController.class);
    private final ChatClient chatClient;
    private final EmbeddingModel embeddingModel;

    public OpenAiChatController(ChatClient.Builder chatClientBuilder, EmbeddingModel embeddingModel) {
        this.chatClient = chatClientBuilder.build();
        this.embeddingModel = embeddingModel;
    }

    /**
     * 对话
     * @param message
     * @return
     */
    @GetMapping("/chat")
    public String chat(String message) {
        return this.chatClient.prompt()
                .user(message)
                .call()
                .content();
    }

    /**
     * 向量化
     * @param message
     * @return
     */
    @GetMapping("/embedding")
    public EmbeddingResponse embedding(String message) {
        return this.embeddingModel.embedForResponse(List.of(message));
    }

}
